package com.sdses.weather.config.mapping;

import com.sdses.weather.config.WeatherProviderSchemaConfig;
import org.springframework.context.ApplicationContext;

import java.text.SimpleDateFormat;
import java.util.Date;

public class SpringAwareConverter implements FieldConverter<Object> {

    private final WeatherProviderSchemaConfig.FieldMapping field;
    private final ApplicationContext applicationContext;
    private final java.util.logging.Logger log = java.util.logging.Logger.getLogger(SpringAwareConverter.class.getName());

    public SpringAwareConverter(
            WeatherProviderSchemaConfig.FieldMapping field,
            ApplicationContext applicationContext) {
        this.field = field;
        this.applicationContext = applicationContext;
    }

    @Override
    public Object convert(Object value, String provider) {
        if (value == null) {
            return null;
        }

        // 1. 自定义转换器
        if (field.getConverter() != null && !field.getConverter().trim().isEmpty()) {
            try {
                Object converterBean = applicationContext.getBean(field.getConverter().trim());
                if (converterBean instanceof FieldConverter) {
                    @SuppressWarnings("unchecked")
                    FieldConverter<Object> converter = (FieldConverter<Object>) converterBean;
                    return converter.convert(value, provider);
                }
            } catch (Exception e) {
                log.severe("转换器调用失败: " + field.getConverter() + ", 错误: " + e.getMessage());
            }
        }

        // 2. 默认类型转换
        String type = field.getType();
        String strValue = value.toString().trim();

        try {
            if ("string".equalsIgnoreCase(type)) {
                return strValue;
            } else if ("int".equalsIgnoreCase(type) || "integer".equalsIgnoreCase(type)) {
                return Integer.valueOf(strValue);
            } else if ("long".equalsIgnoreCase(type)) {
                return Long.valueOf(strValue);
            } else if ("double".equalsIgnoreCase(type) || "float".equalsIgnoreCase(type)) {
                return Double.valueOf(strValue);
            } else if ("boolean".equalsIgnoreCase(type)) {
                return Boolean.valueOf(strValue);
            } else if ("date".equalsIgnoreCase(type)) {
                String format = field.getSortFormat(); // 假设你有 getSortFormat()
                if (format != null && !format.trim().isEmpty()) {
                    try {
                        return new SimpleDateFormat(format.trim()).parse(strValue);
                    } catch (Exception e) {
                        log.severe("日期格式转换失败: format=" + format + ", value=" + strValue);
                    }
                }
                return new Date(); // 默认返回当前时间
            } else if ("enum".equalsIgnoreCase(type)) {
                log.warning("enum 类型应由转换器处理，字段: " + field.getJava());
                return null;
            } else {
                return value;
            }
        } catch (Exception e) {
            log.severe("类型转换失败: type=" + type + ", value=" + strValue + ", 错误: " + e.getMessage());
            return null;
        }
    }
}